package com.controller;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.context.ManagerContext;
import com.domain.dto.PageDTO;
import com.domain.dto.ProductsDTO;
import com.domain.dto.ProxyDto;
import com.domain.dto.StateDTO;
import com.domain.po.ProductCategory;
import com.domain.query.ProductQuery;
import com.domain.query.RecordListQuery;
import com.domain.vo.ProductDetailsVO;
import com.domain.vo.ProductVO;
import com.domain.vo.RecordVO;
import com.result.Result;
import com.result.Results;
import com.service.ProductCategoryService;
import com.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 17645
 * @date 2024/06/16
 */
@RestController
@Slf4j
@RequestMapping("/products")
public class ProductController {


    @SofaReference(interfaceType = ProductCategoryService.class,  binding = @SofaReferenceBinding(bindingType = "bolt"))
    private ProductCategoryService productCategoryService;
    @SofaReference(interfaceType = ProductService.class, binding = @SofaReferenceBinding(bindingType = "bolt"))
    private ProductService productService;


    /**
     * 修改商品信息
     *
     * @param productsDTO 商品信息
     * @return 返回信息
     */
    @PutMapping("/update")
    public Result<Void> updateProducts(@RequestBody ProductsDTO productsDTO) {
        log.info("修改商品：{}", productsDTO);
        //获取当前登录者id
        productsDTO.setManagerId(ManagerContext.getManagerId());
        productService.updateProduct(productsDTO);
        return Results.success();
    }

    /**
     * 修改商品状态
     *
     * @param stateDTO
     * @return
     */
    @PutMapping("/produceState")
    public Result updateState(@RequestBody StateDTO stateDTO) {
        log.info("修改商品状态：{}", stateDTO);
        //获取登陆人id
        stateDTO.setOperationId(ManagerContext.getManagerId());
        productService.updateState(stateDTO);
        return Results.success();
    }

    /**
     * 查看商品操作记录
     *
     * @param recordListQuery
     * @return
     */
    @GetMapping("/recordList")
    public Result<PageDTO<RecordVO>> recordListPage(RecordListQuery recordListQuery) {
        log.info("查看商品操作记录：{}", recordListQuery);
        PageDTO<RecordVO> pageDTO = productService.recordListQuery(recordListQuery);
        return Results.success(pageDTO);
    }


    /**
     * 查询所有分类接口
     *
     * @return 返回分类信息
     */
    @GetMapping("/categoryList")
    public Result<List<ProductCategory>> categoryList() {
        log.info("查询商品分类");

        List<ProductCategory> productCategoryList = productCategoryService.list();
        return Results.success(productCategoryList);
    }

    /**
     * 新增商品信息
     *
     * @param productsDTO 商品信息
     * @return 返回信息
     */
    @PostMapping("/create")
    public Result<Void> createProduct(@RequestBody ProductsDTO productsDTO) {

        //获取当前登陆人信息
        Long managerId = ManagerContext.getManagerId();
        String managerName = ManagerContext.getUsername();
        productsDTO.setManagerId(managerId);
        productsDTO.setManagerName(managerName);
        log.info("新增商品信息：{}", productsDTO);
        productService.createProduct(productsDTO);
        return Results.success();
    }

    /**
     * 通过商品id查询商品详情信息
     *
     * @param productId 商品Id
     * @return 返回商品详情信息
     */
    @GetMapping("/view")
    public Result<ProductDetailsVO> queryByProductId(Long productId) {
        log.info("通过商品id查询商品详情信息{}",productId);
        ProductDetailsVO productDetailsVO = productService.queryByProductId(productId);
        return Results.success(productDetailsVO);
    }

    /**
     * 商品基本信息分页查询
     *
     * @param productQuery 分页查询参数
     * @return 分页查询结果
     */
    @GetMapping("/list")
    public Result<PageDTO<ProductVO>> productListPage(ProductQuery productQuery) {
        productQuery.setManagerId(ManagerContext.getManagerId());
        log.info("查询商品基本信息列表：{}", productQuery);
        PageDTO<ProductVO> pageDTO = productService.productListPage(productQuery);
        return Results.success(pageDTO);
    }

    @GetMapping("/testError")
    public void TestError() {
        throw new IllegalArgumentException("日期不能超过60天");

    }

    @PostMapping
    public Result updateProxy(List<ProxyDto> proxyDto){
        log.info("修改商品代理人信息：{}", proxyDto);
        productService.updateProxy(proxyDto);
        return Results.success();
    }

}

